@@ -8,6 +8,8 @@ module Agents |
||
8 | 8 |
A PostAgent receives events from other agents (or runs periodically), merges those events with the contents of `payload`, and sends the results as POST (or GET) requests to a specified url. |
9 | 9 |
|
10 | 10 |
The `post_url` field must specify where you would like to send requests. Please include the URI scheme (`http` or `https`). |
11 |
+ |
|
12 |
+ The `headers` field is optional. When present, it should be a hash of headers to send with the request. |
|
11 | 13 |
MD |
12 | 14 |
|
13 | 15 |
event_description "Does not produce events." |
@@ -19,7 +21,8 @@ module Agents |
||
19 | 21 |
'method' => 'post', |
20 | 22 |
'payload' => { |
21 | 23 |
'key' => 'value' |
22 |
- } |
|
24 |
+ }, |
|
25 |
+ 'headers' => {} |
|
23 | 26 |
} |
24 | 27 |
end |
25 | 28 |
|
@@ -31,6 +34,10 @@ module Agents |
||
31 | 34 |
(options['method'].presence || 'post').to_s.downcase |
32 | 35 |
end |
33 | 36 |
|
37 |
+ def headers |
|
38 |
+ options['headers'].presence || {} |
|
39 |
+ end |
|
40 |
+ |
|
34 | 41 |
def validate_options |
35 | 42 |
unless options['post_url'].present? && options['expected_receive_period_in_days'].present? |
36 | 43 |
errors.add(:base, "post_url and expected_receive_period_in_days are required fields") |
@@ -43,6 +50,10 @@ module Agents |
||
43 | 50 |
unless %w[post get].include?(method) |
44 | 51 |
errors.add(:base, "method must be 'post' or 'get'") |
45 | 52 |
end |
53 |
+ |
|
54 |
+ unless headers.is_a?(Hash) |
|
55 |
+ errors.add(:base, "if provided, headers must be a hash") |
|
56 |
+ end |
|
46 | 57 |
end |
47 | 58 |
|
48 | 59 |
def receive(incoming_events) |
@@ -75,14 +86,14 @@ module Agents |
||
75 | 86 |
|
76 | 87 |
def post_data(data) |
77 | 88 |
uri = generate_uri |
78 |
- req = Net::HTTP::Post.new(uri.request_uri) |
|
89 |
+ req = Net::HTTP::Post.new(uri.request_uri, headers) |
|
79 | 90 |
req.form_data = data |
80 | 91 |
Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == "https") { |http| http.request(req) } |
81 | 92 |
end |
82 | 93 |
|
83 | 94 |
def get_data(data) |
84 | 95 |
uri = generate_uri(data) |
85 |
- req = Net::HTTP::Get.new(uri.request_uri) |
|
96 |
+ req = Net::HTTP::Get.new(uri.request_uri, headers) |
|
86 | 97 |
Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == "https") { |http| http.request(req) } |
87 | 98 |
end |
88 | 99 |
end |
@@ -143,6 +143,23 @@ describe Agents::PostAgent do |
||
143 | 143 |
@checker.options['payload'] = { 'this' => 'that' } |
144 | 144 |
@checker.should be_valid |
145 | 145 |
end |
146 |
+ |
|
147 |
+ it "requires headers to be a hash, if present" do |
|
148 |
+ @checker.options['headers'] = [1,2,3] |
|
149 |
+ @checker.should_not be_valid |
|
150 |
+ |
|
151 |
+ @checker.options['headers'] = "hello world" |
|
152 |
+ @checker.should_not be_valid |
|
153 |
+ |
|
154 |
+ @checker.options['headers'] = "" |
|
155 |
+ @checker.should be_valid |
|
156 |
+ |
|
157 |
+ @checker.options['headers'] = {} |
|
158 |
+ @checker.should be_valid |
|
159 |
+ |
|
160 |
+ @checker.options['headers'] = { "Authorization" => "foo bar" } |
|
161 |
+ @checker.should be_valid |
|
162 |
+ end |
|
146 | 163 |
end |
147 | 164 |
|
148 | 165 |
describe "#generate_uri" do |